package com.git.contractmanagement.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.git.contractmanagement.config.BaseContext;
import com.git.contractmanagement.entity.Orders;
import com.git.contractmanagement.entity.dto.OrdersPageQueryDTO;
import com.git.contractmanagement.entity.dto.OrdersSubmitDTO;
import com.git.contractmanagement.entity.result.PageResult;
import com.git.contractmanagement.entity.result.Result;
import com.git.contractmanagement.entity.vo.OrdersSubmitVO;
import com.git.contractmanagement.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController("userOrderController")
@RequestMapping("/user/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;


    /**
     * 提交订单
     * @param ordersSubmitDTO
     * @return
     */
    @PostMapping("/submit")
    public Result<OrdersSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        Long userId = Long.parseLong(StpUtil.getLoginId().toString());
        BaseContext.setCurrentId(userId);
        OrdersSubmitVO ordersSubmitVO = orderService.submitUserOrder(ordersSubmitDTO);
        return Result.success(ordersSubmitVO);
    }


    /**
     * 获取订单详情
     * @param orderId
     * @return
     */
    @GetMapping("/{orderId}")
    public Result<Orders> getOrder(@PathVariable Long orderId) {
        // 实现获取订单的逻辑
        Orders orders = orderService.getOrder(orderId);
        return Result.success(orders);
    }


    /**
     * 取消订单
     * @param orderId
     * @return
     */
    @PostMapping("/cancel/{orderId}")
    public Result<String> cancelOrder(@PathVariable Long orderId) {
        // 实现取消订单的逻辑
        orderService.cancelOrder(orderId);
        return Result.success("取消订单成功");
    }

    /**
     * 获取历史订单
     * @param page
     * @param pageSize
     * @param status
     * @return
     */


    @GetMapping("/historyOrders")
    public Result<PageResult> page(int page, int pageSize, Integer status) {
        Long userId = Long.parseLong(StpUtil.getLoginId().toString());
        BaseContext.setCurrentId(userId);
        PageResult pageResult = orderService.pageQueryUser(page, pageSize, status);
        return Result.success(pageResult);
    }

    /**
     * 获取满足条件的订单
     * @param ordersPageQueryDTO
     * @return
     */
    @PostMapping("/conditionOrders")
    public Result<PageResult> conditionPage(@RequestBody OrdersPageQueryDTO ordersPageQueryDTO) {
        Long userId = Long.parseLong(StpUtil.getLoginId().toString());
        BaseContext.setCurrentId(userId);
        PageResult pageResult = orderService.pageQuery4User(ordersPageQueryDTO);

        return Result.success(pageResult);
    }


    /**
     * 完成订单
     * @param orderId
     * @return
     */
    @PostMapping("/complet/{orderId}")
    public Result complet(@PathVariable Long orderId) {

        orderService.complet(orderId);

        return Result.success();
    }




}
